package com.iking.housekeeping.controller.user;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import com.iking.housekeeping.redis.RedisObjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.iking.housekeeping.common.CodeEnum;
import com.iking.housekeeping.common.ReturnCode;
import com.iking.housekeeping.entity.dtuser.DeptUser;
import com.iking.housekeeping.entity.user.SysUser;
import com.iking.housekeeping.httpbean.user.reqbean.DelUser;
import com.iking.housekeeping.httpbean.user.reqbean.InsertUser;
import com.iking.housekeeping.service.dept.DeptService;
import com.iking.housekeeping.service.dtuser.DeptUserService;
import com.iking.housekeeping.service.user.UserService;
import com.iking.housekeeping.util.MessageDigest5;
import com.iking.housekeeping.util.UUIDUtil;
import com.iking.housekeeping.util.token.WebTokenUtil;

/**
 * 用户管理Controller
 * 
 * @author 张重阳
 *
 */
@RestController
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserService userService;
	
	@Autowired
	private DeptService deptService;
	
	@Autowired
	private DeptUserService deptUserService;

	@Autowired
	private RedisObjectService<String> redisObjectService;
	/**
	 * 添加用户信息
	 * 
	 * @param sysUser
	 * @param bindingResult
	 * @return
	 */
	@Transactional
	@PostMapping("/addUser")
	public ReturnCode addUser(@RequestBody @Valid InsertUser insertUser, BindingResult bindingResult) {
		// 判断参数是否合法
		if (bindingResult.hasErrors()) {
			String msg = bindingResult.getFieldError().getDefaultMessage();
			return new ReturnCode(CodeEnum.FAIL, msg, null);
		}
		// 判断用户名是否存在
		if (userService.selectUserByName(insertUser.getfLoginName()) != null) {
			return new ReturnCode(CodeEnum.FAIL, "用户名已存在", null);
		}
		// 添加系统用户
		SysUser sysUser = new SysUser(UUIDUtil.getUUID(),insertUser.getfLoginName(), insertUser.getfPassword());
		sysUser.setfUserName(insertUser.getfUserName());
		sysUser.setfStatus(1);
		Integer ret = userService.addSysUser(sysUser);
		if (ret == 0) {
			return new ReturnCode(CodeEnum.FAIL);
		}
		// 创建部门用户关系
		List<DeptUser> depts = new ArrayList<DeptUser>();
		for (String deptId : insertUser.getListDept()) {
			DeptUser depruser = new DeptUser(UUIDUtil.getUUID(),deptId, sysUser.getfId());
			depts.add(depruser);
		}	
		//添加用户
		deptUserService.insertDeptUser(depts);
		return new ReturnCode(CodeEnum.SUCCESS);
	}

	/**
	 * 删除用户
	 */
	@Transactional
	@PostMapping("/delUser")
	public ReturnCode delUser(@RequestBody DelUser delUser) {
		List<String> list= delUser.getUserIdList();
		for (int i = 0; i <list.size(); i++) {
			String id=redisObjectService.get("webToken:" +list.get(i));
			if(id!=null){
				return new ReturnCode(CodeEnum.FAIL, "正在登录的用户不能删除！", null);
			}
		}
		//将用户表中的数据逻辑删除
		userService.delUserByIdList(delUser.getUserIdList());
		//物理删除用户与组织的关联
		deptUserService.delDeptUserByUserIdList(delUser.getUserIdList());
		//如果用户为管理员，则将原管理员设置为空字符串
		deptService.selectByAdmin(delUser.getUserIdList());
		return new ReturnCode(CodeEnum.SUCCESS);
	}

	/**
	 * 修改用户
	 */
	@PostMapping("/updateUserById")
	public ReturnCode updateUserById(@RequestBody @Valid SysUser sysUser, BindingResult bindingResult) {
		// 判断参数是否合法
		if (bindingResult.hasErrors()) {
			String msg = bindingResult.getFieldError().getDefaultMessage();
			return new ReturnCode(CodeEnum.FAIL, msg);
		}
		// 判断用户名是否存在
		SysUser sysUser2 = userService.selectUserByName(sysUser.getfLoginName());
		if (sysUser2 != null && !sysUser2.getfId().equals(sysUser.getfId())) {
			return new ReturnCode(CodeEnum.FAIL, "用户名已存在", null);
		}
		// 对密码进行加密
		sysUser.setfPassword(MessageDigest5.md5(sysUser.getfPassword()));
		Integer restul = userService.updateUserByID(sysUser);
		if (restul != 1) {
			return new ReturnCode(CodeEnum.FAIL);
		}
		return new ReturnCode(CodeEnum.SUCCESS);
	}

	/**
	 * 根据查询用户
	 */
	@GetMapping("/selectUserById/{fId}")
	public ReturnCode selectUserById(@PathVariable("fId") String fId) {
		SysUser sysUser = userService.selectUserById(fId);
		if (sysUser == null) {
			return new ReturnCode(CodeEnum.NOT_DATA);
		}
		return new ReturnCode(CodeEnum.SUCCESS, sysUser);
	}
	
	/**
	 * 根据查询用户
	 */
	@GetMapping("/getUserInfo")
	public ReturnCode getUserInfo(HttpServletRequest request) {
		String fId = WebTokenUtil.getUserIdByRequest(request);
		SysUser sysUser = userService.selectUserById(fId);
		if (sysUser == null) {
			return new ReturnCode(CodeEnum.NOT_DATA);
		}
		return new ReturnCode(CodeEnum.SUCCESS, sysUser);
	}
	
	
}
